package org.argeproje.resim;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchWindow;

public class WindowUT {
	public static String saveFileDialog(String extension, IWorkbenchWindow window, String dlgName) {
		FileDialog dialog = new FileDialog(window.getShell(), SWT.SAVE);
		dialog.setText(dlgName);
		dialog.setFilterExtensions(new String[] { "*." + extension });
	    boolean done = false;
        String fileName = null;
	    while (!done) {
	      // Open the File Dialog
	      fileName = dialog.open();
	      if (fileName == null) {
	        // User has cancelled, so quit and return
	        done = true;
	      } else {
	        // User has selected a file; see if it already exists
	    	if(!fileName.matches(".*\\." + extension)){
	    		fileName = fileName + "." + extension;
	    	}
	        File file = new File(fileName);
	        if (file.exists()) {
	          // The file already exists; asks for confirmation
	          MessageBox mb = new MessageBox(dialog.getParent(), SWT.ICON_WARNING
	              | SWT.YES | SWT.NO);

	          // We really should read this string from a
	          // resource bundle
	          mb.setMessage(fileName + " already exists. Do you want to replace it?");

	          // If they click Yes, we're done and we drop out. If
	          // they click No, we redisplay the File Dialog
	          done = mb.open() == SWT.YES;
	        } else {
	          // File does not exist, so drop out
	          done = true;
	        }
	      }
	    }
	    return fileName;
	}
	public static String openFileDialog(String extension, IWorkbenchWindow window) {
		FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN);
		dialog.setText("Open");
		dialog.setFilterExtensions(new String[] { "*." + extension });
		return dialog.open();
	}
}
